<html>
 <head>
  <meta charset="UTF-8">
 </head>
 <body>
  <h1 data-lake-id="u6zeP" id="u6zeP"><span data-lake-id="u6d486a8b" id="u6d486a8b">典型回答</span></h1>
  <p data-lake-id="ue84ab075" id="ue84ab075"><span data-lake-id="u4a1ab376" id="u4a1ab376" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">如一条简单的查询语句：</span><code data-lake-id="u9819cb72" id="u9819cb72"><span data-lake-id="u3f9c1ab9" id="u3f9c1ab9" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">select * from users where age='18' and name='Hollis';</span></code></p>
  <p data-lake-id="u1d6409bf" id="u1d6409bf"><span data-lake-id="u677c539c" id="u677c539c" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">​</span><br></p>
  <p data-lake-id="u1f37680d" id="u1f37680d"><span data-lake-id="u8daff244" id="u8daff244" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">执行过程如下图：</span></p>
  <p data-lake-id="u23db4561" id="u23db4561"><span data-lake-id="u40bdf76c" id="u40bdf76c" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">​</span><br></p>
  <p data-lake-id="u20d6cf73" id="u20d6cf73"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1676276921091-c44ad9b7-f173-4099-9bed-39486d5dbd07.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_27%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="u234926ea" id="u234926ea"><span data-lake-id="ud77d5f2b" id="ud77d5f2b" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">​</span><br></p>
  <p data-lake-id="uc48ec4b8" id="uc48ec4b8"><span data-lake-id="ucc814ac2" id="ucc814ac2" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">结合上面的说明，我们分析下这个语句的执行流程：</span></p>
  <p data-lake-id="u9d4c686f" id="u9d4c686f"><span data-lake-id="u7e42b01a" id="u7e42b01a" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">①使用</span><strong><span data-lake-id="ue15c76a7" id="ue15c76a7" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">连接器</span></strong><span data-lake-id="uea1061b8" id="uea1061b8" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">，通过客户端/服务器通信协议与 MySQL 建立连接。并查询是否有权限</span></p>
  <p data-lake-id="u02a8678e" id="u02a8678e"><span data-lake-id="uaec11d6a" id="uaec11d6a" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">②Mysql8.0之前</span><strong><span data-lake-id="u9b7e8a8a" id="u9b7e8a8a" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">检查是否开启缓存</span></strong><span data-lake-id="u15a18995" id="u15a18995" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">，开启了 Query Cache 且命中完全相同的 SQL 语句，则将查询结果直接返回给客户端；</span></p>
  <p data-lake-id="u8800c0f8" id="u8800c0f8"><span data-lake-id="ucede7ed2" id="ucede7ed2" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">③由</span><strong><span data-lake-id="u5f4eac5d" id="u5f4eac5d" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">解析器（分析器）</span></strong><span data-lake-id="u4fe1c0b3" id="u4fe1c0b3" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">进行语法分析和语义分析，并生成解析树。如查询是select、表名users、条件是age='18' and name='Hollis'，</span><strong><span data-lake-id="ud697c673" id="ud697c673">预处理器</span></strong><span data-lake-id="uec5eea47" id="uec5eea47">则会根据 MySQL 规则进一步检查解析树是否合法。比如检查要查询的数据表或数据列是否存在等。</span></p>
  <p data-lake-id="u12a5df8b" id="u12a5df8b"><span data-lake-id="u30e3318b" id="u30e3318b" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">④由</span><strong><span data-lake-id="u839da6f4" id="u839da6f4" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">优化器</span></strong><span data-lake-id="u0a903c41" id="u0a903c41" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">生成执行计划。根据索引看看是否可以优化</span></p>
  <p data-lake-id="u3ec6a5b6" id="u3ec6a5b6"><span data-lake-id="u72897adc" id="u72897adc" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">⑤</span><strong><span data-lake-id="ueccea705" id="ueccea705" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">执行器</span></strong><span data-lake-id="u6c3672b7" id="u6c3672b7" class="lake-fontsize-12" style="color: rgba(25, 26, 31, 0.9)">来执行SQL语句，这里具体的执行会操作MySQL的存储引擎来执行 SQL 语句，根据存储引擎类型，得到查询结果。若开启了 Query Cache，则缓存，否则直接返回。</span></p>
  <p data-lake-id="ufeed9fcf" id="ufeed9fcf"><span data-lake-id="u729a11fa" id="u729a11fa"><br><br></span></p>
 </body>
</html>